package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.constant.EnergyLevelType;
import com.juyu.miliao.api.domain.Appkey;
import com.juyu.miliao.api.domain.vo.FindDynamicShareVO;
import com.juyu.miliao.api.domain.vo.UserInvitationIncomeByInviteUserDetailsVO;
import com.juyu.miliao.api.domain.vo.UserInvitationIncomeDetailsVO;
import com.juyu.miliao.api.domain.vo.UserInvitationShareVO;
import com.juyu.miliao.api.service.AppKeyService;
import com.juyu.miliao.api.service.UserInvitationIncomeDetailsService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuanqicheng
 */
@RestController
@RequestMapping("/invitation")
public class UserInvitationIncomeDetailsController {

    @Autowired
    private UserInvitationIncomeDetailsService userInvitationIncomeDetailsService;

    @Autowired
    private AppKeyService appKeyService;

    /**
     * 更加明细类型查询分页列表
     *
     * @param token
     * @param page
     * @param pageSize
     * @param detailType 明细类型：0：邀请用户赚取类型，1：邀请主播赚钱类型
     * @return
     */
    @GetMapping("/{detailType}/{page}-{pageSize}")
    public List<UserInvitationIncomeDetailsVO> list(Authentication token, @PathVariable("page") Integer page,
                                                    @PathVariable("pageSize") Integer pageSize,
                                                    @PathVariable("detailType") int detailType) {
        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }

        Subject subject = (Subject) token.getPrincipal();

        return userInvitationIncomeDetailsService
                .getUserInvitationIncomeDetailsByInviteUserId(subject.getId(), page, pageSize, detailType);
    }

    /**
     * 邀请用户收益汇总
     *
     * @param token
     * @return
     */
    @GetMapping("/income")
    public UserInvitationIncomeByInviteUserDetailsVO getIncome(Authentication token) {
        Subject subject = (Subject) token.getPrincipal();

        return userInvitationIncomeDetailsService.getIncomeByInviteUserId(subject.getId());
    }

    /**
     * 分享
     *
     * @param token
     * @param type  1: 邀请主播，2：邀请用户
     * @return
     * @throws IOException
     */
    @GetMapping("/share/{type}")
    public UserInvitationShareVO getShareByDynamicId(Authentication token, @PathVariable("type") Integer type) throws IOException {
        if (type == null || type == 0) {
            throw new ConsumeRuntimeException("分享类型不正确");
        }

        Subject subject = (Subject) token.getPrincipal();

        long userId = subject.getId();

        Appkey app = appKeyService.getAppKeyByKey("userInvitationShareUrl");

        if (type == 1) {
            String url = app.getValue() + "#/share?uid=" + userId;
            UserInvitationShareVO vo = new UserInvitationShareVO();
            Appkey iconKey = appKeyService.getAppKeyByKey("xunxun-host-icon");
            vo.setTitle("视频聊天还能轻松赚钱？");
            vo.setContent("快来寻寻开启主播之旅");
            vo.setIcon(iconKey.getValue());
            vo.setUrl(url);
            return vo;
        }else{
            String url = app.getValue()+"?uid=" + userId;
            UserInvitationShareVO vo = new UserInvitationShareVO();
            Appkey iconKey = appKeyService.getAppKeyByKey("xunxun-yonghu-icon");
            vo.setTitle("有趣又有料");
            vo.setContent("一大波性感小姐姐在等你");
            vo.setIcon(iconKey.getValue());
            vo.setUrl(url);
            return vo;
        }
    }

    /**
     * 复制链接
     * @param token
     * @param type  1: 邀请主播，2：邀请用户
     * @return
     * @throws IOException
     */
    @GetMapping("/copy/link/{type}")
    public Map<String,Object> getCopyLink(Authentication token, @PathVariable("type") Integer type){
        if (type == null || type == 0) {
            throw new ConsumeRuntimeException("分享类型不正确");
        }

        Map<String,Object> vo = new HashMap<>(1);

        Subject subject = (Subject) token.getPrincipal();

        long userId = subject.getId();

        Appkey app = appKeyService.getAppKeyByKey("userInvitationShareUrl");
        if(type==1){
            String url = app.getValue() + "#/share?uid=" + userId;
            vo.put("url",url);
        }else if(type==2) {
            String url = app.getValue()+"?uid=" + userId;
            vo.put("url",url);
        }

        return vo;
    }

}
